﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Protocol;

namespace MDS
{
    public class EtfQuotationProvider : IEtfQuotationProvider
    {

        public event MessageHandler<Protocol.GWMKTETFListANS> EtfListMsgReceived;


        protected EtfQuotationProviderConfig _config;
        protected System.Timers.Timer _refreshTimer;
        protected bool _isReading = false;
        protected Dictionary<string, DateTime> _etfFileWriteTimeDic = new Dictionary<string, DateTime>();
        protected Dictionary<string, GWMKTETFListANS> _snapshot = new Dictionary<string, GWMKTETFListANS>();

        public EtfQuotationProvider(EtfQuotationProviderConfig config)
        {
            this._config = config;
            this._refreshTimer = new System.Timers.Timer()
            {
                Interval = this._config.CheckIntervalMS,
                AutoReset = false,
                Enabled = false
            };
            this._refreshTimer.Elapsed += new System.Timers.ElapsedEventHandler(_refreshTimer_Elapsed);
        }

        protected void _refreshTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (this._isReading)
                return;
            this._isReading = true;
            try
            {
                foreach (var item in this._config.Items)
                {
                    if (File.Exists(item.FileName))
                    {
                        string etfListContent = File.ReadAllText(item.FileName);
                        IEtfListParser etfListParser = EtfListParserFactory.GetEtfListParser(item);
                        var etfListAns = etfListParser.Parse(etfListContent, item.Exch);
                    }
                }

            }
            catch (Exception ex)
            {

            }
            finally
            {
                this._isReading = false;
            }
        }

        protected void updateSnapshot(GWMKTETFListANS etfListAns)
        {
            this._snapshot[etfListAns.FundID] = etfListAns;
        }
        protected void onEtfListMsgReceived(GWMKTETFListANS etfListAns)
        {
            MessageHandler<Protocol.GWMKTETFListANS> handler = this.EtfListMsgReceived;
            if (handler != null)
                handler(etfListAns);
        }

        public GWMKTETFListANS GetGWMKTETFListANS(string fundID)
        {
            if (this._snapshot.ContainsKey(fundID))
            {
                return this._snapshot[fundID];
            }
            else
                return null;
        }


        public void Start()
        {
            this._refreshTimer.Enabled = true;
        }

        public void Stop()
        {
            this._refreshTimer.Enabled = false;
        }

        public void Clear()
        {
            this._snapshot.Clear();
            this._etfFileWriteTimeDic.Clear();
        }
    }
}
